package com.finger.forest.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author jzj
 * @date 2020/11/4
 * @desc
 */
public class VoiceWidthUtil {

    /**
     * 根据时间秒计算气泡宽度
     * @param second
     * @return
     */
    public static int calVoiceWidth(int second) {
        int width = 200;
        if (second >= 2){
            width += second*20;
        }

        return width<400? width : 400;
    }

    /**
     * 根据时间秒计算气泡宽度
     * @param size
     * @return
     */
    public static int calVoiceWidth1(int size) {
        int width = 100;
        size = size/10000;
        if (size < 10){
            width = 100;
        } else {
            width += size*2;
        }
        return width<400? width : 400;
    }

    public static void main(String[] args) {
        System.out.println(calVoiceWidth1(511532));
        System.out.println(calVoiceWidth1(16482));
    }

    public static File downloadWxImage(String url) {
        File file = null;

        URL urlfile;
        InputStream inputStream = null;
        OutputStream outputStream= null;
        try {
            file = File.createTempFile("xunhu", ".pcm");
            //下载
            urlfile = new URL(url);
            inputStream = urlfile.openStream();
            outputStream= new FileOutputStream(file);

            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != outputStream) {
                    outputStream.close();
                }
                if (null != inputStream) {
                    inputStream.close();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return file;
    }
}
